home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / laptop-mode-tools / modules / hdparm < prev    next >
Encoding:
Text File  |  2012-05-20  |  8.1 KB  |  289 lines

  1. #! /bin/sh
  2. #
  3. # Laptop mode tools module: adjust hard drive powermanagement settings
  4. #
  5. # This is a core module that takes its configuration from the main config
  6. # file.
  7. #
  8.  
  9. #
  10. # Function for drive capability check. This prevents ugly errors in
  11. # the kernel output about unsupported commands.
  12. #
  13. # $1 = drive name
  14. # $2 = capability (SDPARM/HDPARM or IDLE_TIMEOUT/POWERMGMT/WRITECACHE)
  15. is_capable() {
  16.     local dev=${1#/dev/}
  17.     local MEDIA=
  18.     local BUS=
  19.     
  20.     # Make sure the drive exists before checking anything.
  21.     if ! [ -e $1 ]; then
  22.         return 1;
  23.     fi
  24.  
  25.     if [ -n "$(hdparm -I $1 | grep SSD)" ]; then
  26.         # If the device is a SSD, it is not capable of power management.
  27.         if [ "$2" = "POWERMGMT" ]; then
  28.             log "VERBOSE" "$1 is a SSD, not capable of power management"
  29.             return 1
  30.         fi
  31.     fi
  32.  
  33.     # If we are running udev, this is the most portable way
  34.     # It assumes more or less recent udev (> 070)
  35.     if [ $HAVE_UDEVINFO -ne 0 ] ; then
  36.         log "VERBOSE" "Querying $1 media type using udevinfo: "
  37.         if [ -x /sbin/udevadm ]; then
  38.             eval "$(udevadm info -q env -n $1 | egrep '(ID_TYPE=|ID_BUS=)' )"
  39.         else
  40.             eval "$(udevinfo -q env -n $1 | egrep '(ID_TYPE=|ID_BUS=)' )"
  41.         fi
  42.         if [ -n "$ID_TYPE" -a -n "$ID_BUS" ] ; then
  43.             log "VERBOSE" "type '$ID_TYPE' on bus '$ID_BUS' detected"
  44.             MEDIA=$ID_TYPE
  45.             BUS=$ID_BUS
  46.         else
  47.             log "ERR" "failed - udev not active?"
  48.         fi
  49.     fi
  50.  
  51.     if [ -z "$MEDIA" ] ; then
  52.         log "VERBOSE" "Querying $1 media type using device name: "
  53.         case $dev in
  54.             hd*)    # IDE device
  55.                 if [ -r /proc/ide/$dev/media ]; then
  56.                     MEDIA="$(cat /proc/ide/$dev/media)"
  57.                     BUS=ata
  58.                     if [ "$MEDIA" = cdrom ] ; then
  59.                         MEDIA=cd
  60.                     fi
  61.                 fi
  62.             ;;
  63.             sd*)    # SCSI disk
  64.                 # No need to check, sd is always SCSI disk
  65.                 MEDIA=disk
  66.                 BUS=scsi
  67.             ;;
  68.             sr* | scd* )
  69.                 # No need to check, sr or scd is always SCSI CD-ROM
  70.                 MEDIA=cd
  71.                 BUS=scsi
  72.             ;;
  73.  
  74.         esac
  75.         if [ -n "$MEDIA" ] ; then
  76.             log "VERBOSE" "type '$MEDIA' on bus '$BUS' detected"
  77.         else
  78.             log "ERR" "failed - unknown name"
  79.         fi
  80.     fi
  81.  
  82.     if [ -z "$MEDIA" ] ; then
  83.         if [ "$HDPARM_AVAILABLE" = "1" ]; then
  84.             log "VERBOSE" "Querying $1 type using hdparm: "
  85.             if hdparm -I $1 | grep -q CD-ROM ; then
  86.                 MEDIA=cd
  87.             else
  88.                 MEDIA=disk
  89.             fi
  90.             BUS=ata # or acts like it anyway, because hdparm supports it.
  91.             log "VERBOSE" "type '$MEDIA' on bus '$BUS' detected"
  92.         fi
  93.     fi
  94.  
  95.     # Sanity check
  96.     if [ -z "$MEDIA" -o -z "$BUS" ] ; then
  97.         log "ERR" "Querying $1 type - unknown type or bus, disabling hdparm/sdparm"
  98.         return 1
  99.     fi
  100.  
  101.     if [ "$BUS" = "scsi" -a "$ASSUME_SCSI_IS_SATA" -ne 0 ] ;then
  102.         # Treat scsi disks as SATA devices. Unfortunately they are hard
  103.         # to recognize -- if anybody has a drive and cares to find out
  104.         # how to recognize them, please enlighten me!
  105.         BUS=ata
  106.     fi
  107.  
  108.     # Now check what capabilities we support for the
  109.     # various media and bus types.
  110.     case "$MEDIA:$BUS:$2" in
  111.         # Although CD-ROM drives usually support
  112.         # idle timeout settings, they don't usually
  113.         # support very low values, and we don't want
  114.         # to mess with that. We simply ignore anything
  115.         # that is a CD player.
  116.         cd:*:* ) return 1;;
  117.  
  118.         # ATA drives support the "hdparm" command but
  119.         # not normally the "sdparm" command.
  120.         *:ata:HDPARM ) return 0 ;;
  121.         *:ata:SDPARM ) return 1 ;;
  122.         
  123.         # SCSI drives support the "sdparm" command, but
  124.         # not normally the "hdparm" command.
  125.         *:scsi:SDPARM ) return 0 ;;
  126.         *:scsi:HDPARM ) return 1 ;;
  127.  
  128.         # On ATA disks everything is supported.
  129.         disk:ata:* ) return 0 ;;
  130.  
  131.         # For sdparm we only know how to set the idle
  132.         # timeout, nothing else at the moment.
  133.         *:scsi:IDLE_TIMEOUT ) return 0 ;;
  134.  
  135.         # No other capabilities are supported.
  136.         * ) return 1 ;;
  137.     esac
  138. }
  139.  
  140.  
  141. # Preparation: determine the tools we have available.
  142. if [ -x "$(which hdparm 2> /dev/null)" ]; then
  143.   HDPARM_AVAILABLE=1
  144. fi
  145. if [ -x "$(which sdparm 2> /dev/null)" ]; then
  146.   SDPARM_AVAILABLE=1
  147. fi
  148. HAVE_UDEVINFO=0
  149. if [ -x "$(which udevadm 2> /dev/null)" ]; then
  150.     UDEVVERSION=$(udevadm info -V)
  151.     UDEV_VER_VERIFY=$(echo $UDEVVERSION | cut -b 1)
  152.     case $UDEV_VER_VERIFY in
  153.         [a-z]) UDEVVERSION=$(udevadm info -V | awk '{print $3}')
  154.         ;;
  155.         *)
  156.         ;;
  157.     esac
  158.  
  159.     if [ "$UDEVVERSION" -gt 70 ] ; then
  160.         HAVE_UDEVINFO=1
  161.     else
  162.         log "VERBOSE" "udevadm info present but version not > 070, not using udev"
  163.     fi
  164. else
  165.     # Older versions of udev (udevinfo) give output in the form of
  166.     # "udevinfo, version 125"
  167.     # Will be removed later. Currently only for backward compatibility
  168.     if [ -x "$(which udevinfo 2> /dev/null)" ] ; then
  169.         UDEVVERSION=$(udevinfo -V | awk '{ print $3; }')
  170.         if [ "$UDEVVERSION" -gt 70 ] ; then
  171.             HAVE_UDEVINFO=1
  172.         else
  173.             log "VERBOSE" "udevinfo present but version not > 070, not using udev"
  174.         fi
  175.     fi
  176. fi
  177.  
  178. if [ x$CONTROL_HD_POWERMGMT = x1 ] || [ x$ENABLE_AUTO_MODULES = x1 -a x$CONTROL_HD_POWERMGMT = xauto ]; then
  179.     if [ $ON_AC -eq 1 ] ; then
  180.         if [ "$ACTIVATE" -eq 1 ] ; then
  181.             HD_POWERMGMT=$LM_AC_HD_POWERMGMT
  182.         else
  183.             HD_POWERMGMT=$NOLM_AC_HD_POWERMGMT
  184.         fi
  185.     else
  186.         HD_POWERMGMT=$BATT_HD_POWERMGMT
  187.     fi
  188.  
  189.     log "VERBOSE" "Setting powermanagement on drives to $HD_POWERMGMT."
  190.     for THISHD in $HD ; do
  191.         if is_capable $THISHD POWERMGMT ; then
  192.             if is_capable $THISHD HDPARM ; then
  193.                 if [ "$HDPARM_AVAILABLE" = "1" ]; then
  194.                     log "VERBOSE" "Executing: hdparm -B $HD_POWERMGMT $THISHD"
  195.                     log "VERBOSE" "`hdparm -B $HD_POWERMGMT $THISHD 2>&1`"
  196.                 else
  197.                     log "ERR" "ERROR: hdparm not installed."                
  198.                 fi
  199.             else
  200.                 log "VERBOSE" "Skipping $THISHD: powermgmt only possible with hdparm but drive does not"
  201.                 log "VERBOSE" "support hdparm."
  202.             fi
  203.         else
  204.             log "VERBOSE" "Skipping $THISHD: powermanagement control not supported."
  205.         fi
  206.     done
  207. fi
  208.  
  209. if [ x$CONTROL_HD_IDLE_TIMEOUT = x1 ] ; then
  210.     # Spindown timeouts may only be set when data-loss sensitive
  211.     # features are active.
  212.     if [ "$ACTIVATE_WITH_POSSIBLE_DATA_LOSS" -eq 1 ] ; then
  213.         if [ $ON_AC -eq 1 ] ; then
  214.             HD_IDLE_TIMEOUT=$LM_AC_HD_IDLE_TIMEOUT
  215.             HD_IDLE_TIMEOUT_SECONDS=$LM_AC_HD_IDLE_TIMEOUT_SECONDS
  216.         else
  217.             HD_IDLE_TIMEOUT=$LM_BATT_HD_IDLE_TIMEOUT
  218.             HD_IDLE_TIMEOUT_SECONDS=$LM_BATT_HD_IDLE_TIMEOUT_SECONDS
  219.         fi
  220.     else
  221.         HD_IDLE_TIMEOUT=$NOLM_HD_IDLE_TIMEOUT
  222.         HD_IDLE_TIMEOUT_SECONDS=$NOLM_HD_IDLE_TIMEOUT_SECONDS
  223.     fi
  224.     log "VERBOSE" "Setting spindown timeout on drives to $HD_IDLE_TIMEOUT_SECONDS seconds."
  225.     log "VERBOSE" "(hdparm configuration value = $HD_IDLE_TIMEOUT.)"
  226.     for THISHD in $HD ; do
  227.         if is_capable $THISHD IDLE_TIMEOUT ; then
  228.             if is_capable $THISHD HDPARM ; then
  229.                 if [ "$HDPARM_AVAILABLE" = "1" ]; then
  230.                     log "VERBOSE" "Executing: hdparm -S $HD_IDLE_TIMEOUT $THISHD"
  231.                     log "VERBOSE" "`hdparm -S $HD_IDLE_TIMEOUT $THISHD 2>&1`"
  232.                 else
  233.                     log "VERBOSE" "ERROR: hdparm not installed."
  234.                 fi
  235.             elif is_capable $THISHD SDPARM ; then
  236.                 if [ "$SDPARM_AVAILABLE" = "1" ]; then
  237.                     HD_IDLE_TIMEOUT_DECISECONDS=$(($HD_IDLE_TIMEOUT_SECONDS*10))
  238.                     log "VERBOSE" "Executing: sdparm -q -s SCT=$HD_IDLE_TIMEOUT_DECISECONDS $THISHD"
  239.                     log "VERBOSE" "`sdparm -q -s SCT=$HD_IDLE_TIMEOUT_DECISECONDS $THISHD 2>&1`"
  240.                 else
  241.                     log "ERR" "ERROR: sdparm not installed."
  242.                 fi
  243.             else
  244.                 log "VERBOSE" "Skipping $THISHD: drive supports neither hdparm nor sdparm."
  245.             fi
  246.         else
  247.             log "VERBOSE" "Skipping $THISHD: idle timeout control not supported."
  248.         fi
  249.     done
  250. fi
  251.  
  252. if [ x$CONTROL_HD_WRITECACHE = x1 ] ; then
  253.     # The writecache may only be enabled when data-loss sensitive
  254.     # features are active.
  255.  
  256.     if [ "$ACTIVATE" -eq 1 ] ; then
  257.         if [ "$ACTIVATE_WITH_POSSIBLE_DATA_LOSS" -eq 0 ] ; then
  258.             HD_WRITECACHE=0
  259.         else
  260.             HD_WRITECACHE=$LM_HD_WRITECACHE
  261.         fi
  262.     else
  263.         if [ $ON_AC -eq 1 ] ; then
  264.             HD_WRITECACHE=$NOLM_AC_HD_WRITECACHE
  265.         else
  266.             HD_WRITECACHE=$NOLM_BATT_HD_WRITECACHE
  267.         fi
  268.     fi
  269.     log "VERBOSE" "Setting write cache on drives to $HD_WRITECACHE."
  270.     for THISHD in $HD ; do
  271.         if is_capable $THISHD WRITECACHE ; then
  272.             if is_capable $THISHD HDPARM ; then
  273.                 if [ "$HDPARM_AVAILABLE" = "1" ]; then
  274.                     log "VERBOSE" "Executing: hdparm -W $HD_WRITECACHE $THISHD"
  275.                     log "VERBOSE" "`hdparm -W $HD_WRITECACHE $THISHD 2>&1`"
  276.                 else
  277.                     log "ERR" "ERROR: hdparm not installed."
  278.                 fi
  279.             else
  280.                 log "VERBOSE" "Skipping $THISHD: writecache only possible with hdparm but drive does not"
  281.                 log "VERBOSE" "support hdparm."
  282.             fi
  283.         else
  284.             log "VERBOSE" "Skipping $THISHD: writecache control not supported."
  285.         fi
  286.     done
  287. fi
  288.  
  289.